package com.autumn.scheduler.server.services;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.autumn.common.api.application.dto.input.PageQueryInput;
import com.autumn.common.api.application.dto.output.IPageQueryResult;
import com.autumn.mybatis.criterion.Query;
import com.autumn.scheduler.SchedulerUtils;
import com.autumn.scheduler.api.QueueSchedulerStatus;
import com.autumn.scheduler.api.services.AutumnJobService;
import com.autumn.scheduler.api.services.dto.input.AutumnJobDetailsAddInput;
import com.autumn.scheduler.api.services.dto.input.AutumnJobDetailsUpdateInput;
import com.autumn.scheduler.api.services.dto.output.AutumnJobDetailsOutput;
import com.autumn.scheduler.api.services.dto.output.AutumnJobLogOutput;
import com.autumn.scheduler.domain.entities.AutumnJobDetails;
import com.autumn.scheduler.domain.services.IJobService;
import com.autumn.scheduler.domain.services.impl.AbstractAutumnSchedulerService;
import com.autumn.util.ExceptionUtils;

@Service(interfaceClass = AutumnJobService.class)
@Component
public class AutumnJobServiceImpl extends AbstractAutumnSchedulerService implements AutumnJobService {

	@Autowired
	private IJobService jobService;

	@Override
	public boolean existJobById(long id) {
		Query query = new Query(AutumnJobDetails.class);
		query.eq("id", id);
		return jobDetailsRepository.countByWhere(query.builderSection()) > 0;
	}

	@Override
	public boolean existJobByName(String jobGroup, String jobName) {
		ExceptionUtils.checkNotNullOrBlank(jobGroup, "jobGroup");
		ExceptionUtils.checkNotNullOrBlank(jobName, "jobName");
		Query query = new Query(AutumnJobDetails.class);
		query.eq("schedName", this.getSchedulerName()).eq("jobFriendlyName", jobName).eq("jobGroup", jobGroup);
		return jobDetailsRepository.countByWhere(query.builderSection()) > 0;
	}

	@Override
	public AutumnJobDetailsOutput addJob(AutumnJobDetailsAddInput input) {
		return jobService.addJob(input);
	}

	@Override
	public boolean deleteJob(long id) {
		return jobService.deleteJob(id);
	}

	@Override
	public void triggerJob(long id) {
		jobService.triggerJob(id);
	}

	@Override
	public int updateJobStatus(QueueSchedulerStatus status) {
		return jobService.updateJobStatus(status);
	}

	@Override
	public boolean pauseJob(long id) {
		return jobService.pauseJob(id);
	}

	@Override
	public int pauseAllJob() {
		return jobService.pauseAllJob();
	}

	@Override
	public boolean resumeJob(long id) {
		return jobService.resumeJob(id);
	}

	@Override
	public int resumeAllJob() {
		return jobService.resumeAllJob();
	}

	@Override
	public AutumnJobDetailsOutput getJob(long id) {
		return jobService.getJob(id);
	}

	@Override
	public AutumnJobDetailsOutput updateJob(AutumnJobDetailsUpdateInput input) {
		return jobService.updateJob(input);
	}

	@Override
	public IPageQueryResult<AutumnJobDetailsOutput> jobQuery(PageQueryInput input) {
		return jobService.jobQuery(input);
	}

	@Override
	public IPageQueryResult<AutumnJobLogOutput> logQuery(PageQueryInput input) {
		return jobService.logQuery(input);
	}

	@Override
	public void clearAllJobLog() {
		jobService.clearAllJobLog();
	}

	@Override
	public List<Date> getJobFireTimeList(String cronExpression, Date startTime, int numTimes) {
		return SchedulerUtils.getJobFireTimeList(cronExpression, startTime, numTimes);
	}

}
